<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in" ref="dialogBox">
      <div class="cornerComment-in-title">
        <span>添加例行工作</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <el-form :model="editForm" label-width="130px" ref="editForm">
          <el-form-item label="例行工作名称：" prop="name">
            <el-input v-model="editForm.name" auto-complete="off" placeholder="填写互动名称"></el-input>
          </el-form-item>
          <el-form-item label="例行工作频率：" prop="name">
            <!--            0 不限 1 每学期 2 每月 3 每周4 每天-->
            <el-radio-group v-model="editForm.type">
              <el-radio label="0">不限</el-radio>
              <el-radio label="1">学期</el-radio>
              <el-radio label="2">月</el-radio>
              <el-radio label="3">周</el-radio>
              <el-radio label="4">日</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="例行工作说明：" prop="remark">
            <el-input v-model="editForm.remark" auto-complete="off" placeholder="填写例行工作说明" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="执行人员：" prop="num">
            <el-button @click.native="getMoreTeach(1)" size="small">选择执行人员（多选）</el-button>
            <el-button @click.native="userType=1,groupVisible=true" size="small">选择执行群组（多选）</el-button>
            <div>
              <el-tag
                  closable
                  :disable-transitions="false"
                  @close="handleCloseExamine(line, 1)"
                  class="mr-10"
                  v-for="(items,line) in dynamicTags"
                  :key="items.id"
              >{{ items.realname }}
              </el-tag>
            </div>
          </el-form-item>
          <el-form-item label="抄送人员：" prop="num">
            <el-button @click.native="getMoreTeach(2)" size="small">选择抄送人员（多选）</el-button>
            <el-button @click.native="userType=2,groupVisible=true" size="small">选择执行群组（多选）</el-button>
            <div>
              <el-tag
                  closable
                  :disable-transitions="false"
                  @close="handleCloseExamine(line, 2)"
                  class="mr-10"
                  v-for="(items,line) in readUserTags"
                  :key="items.id"
              >{{ items.realname }}
              </el-tag>
            </div>
          </el-form-item>
          <el-form-item label="附件：">
            <uploadFileModle number="20" @getFile="getFile" ref="uploadFileModle" :sycnStatus="true"
                             fileType="routine_add" :fileListPush="getList"></uploadFileModle>
          </el-form-item>
        </el-form>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click="closePOpup" size="small">返回</el-button>
        <el-button size="small" type="primary" @click.native="editSubmit">确定并保存</el-button>
      </div>
    </div>
    <!--    单选教职工审核人-->
    <MultipleTeacherModle :visible.sync="singleTeachVisible" @studentFn="studentFn"
                          :dynamicTagsArray="examine_user"></MultipleTeacherModle>
    <!--    选择群组-->
    <groupModule :visible.sync="groupVisible" @studentFn="studentFn"></groupModule>
  </div>
</template>

<script>
import {routineAdd, routineDetail} from "@/api/admin/outineWorke"

export default {
  props: ['visible', 'workId', 'addType'], // commoent 学生点评 collection 学生采集
  components: {},
  data() {
    return {
      singleTeachVisible: false,
      groupVisible: false,
      dialogVisible: false,
      userType: '',
      examine_user: {}, // 审核人
      dynamicTags: [],
      readUserTags: [],
      getList: [],
      editForm: {
        type: '0', // 	整型 	必填 	1 	- 	类型0 不限 1 每学期 2 每月 3 每周4 每天
        name: '', // 	整型 	必填 	1 	- 	名称
        group_id: '', // 	整型 	必填 	1 	- 	用户组
        user_ids: '', // 	整型 	必填 	1 	- 	用户id
        read_user: '', // 抄送人
        file: '', // 	整型 	必填 	1 	- 	模板文件
        remark: '' // 说明
      }, // 编辑界面数据
      // 限制时间选择
      pickerOptions1:
          {
            disabledDate(time) {
              return time.getTime() < Date.now() - 8.64e7
            }
          },
      fileList: []
    }
  },
  watch: {
    visible() {
      this.dialogVisible = this.visible
      if (this.visible) {
        if (this.workId) {
          this.getDetail();
        }
      } else {
        this.editForm = {
          name: '', // 名称
          type: '0',
          descript: '',
          users: '', // 审核人
          end_time: '',
          appendix: '' // 附件
        }
        this.examine_user = {} // 审核人
        this.dynamicTags = []
        this.readUserTags = []
        this.fileList = []
      }
    }
  },
  created() {
  },
  methods: {
    // 打开审核人
    getMoreTeach(option) {
      this.userType = option
      let a = {}
      let b = this.userType == 1 ? JSON.parse(JSON.stringify(this.dynamicTags)) : JSON.parse(JSON.stringify(this.readUserTags));
      for (let item of b) {
        a[item.id] = item
      }
      this.examine_user = this.getJson(a)
      this.singleTeachVisible = true;
    },
    // 删除审核人
    handleCloseExamine(index, type) {
      if (type === 1) {
        this.dynamicTags.splice(index, 1)
      } else {
        this.readUserTags.splice(index, 1)
      }
    },
    // 审核人回执
    studentFn(option) {
      let list = []
      for (let i in option) {
        list.push(option[i])
      }
      if (this.userType == 1) {
        this.dynamicTags = this.getJson(list)
      } else if (this.userType == 2) {
        this.readUserTags = this.getJson(list)
      }
      console.log('提交的参数：', option, this.dynamicTags)
    },
    getDetail() {
      routineDetail({id: this.workId}).then(res => {
        console.log('res:', res)
        if(res.vo.file.indexOf('[') !== -1){
          let file_ = res.vo.file.replace(/src/g,'url')
          this.getList = JSON.parse(file_)
          console.log('文件内容：', this.getList)
        }
        this.dynamicTags = this.getJson(res.vo.user_ids);
        this.readUserTags = res.vo.read_user ? this.getJson(res.vo.read) : []
        this.editForm.type = res.vo.type.toString();
        this.editForm.remark = res.vo.remark;
        // this.editForm.read_user = res.vo.read_user;
        this.editForm.name = res.vo.name;
      })
    },
    // 关闭弹窗
    closePOpup() {
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    getFile(option) {
      console.log('收到附件数据：', option)
      this.fileList = this.getJson(option)
    },
    // 提交数据
    async editSubmit() {
      // 验证信息
      let dataOption = this.getJson(this.editForm)
      console.log('编辑详情', dataOption, this.fileList)
      if (!dataOption.name) {
        this.$message.error('填写工作名称')
        return
      }
      if (!this.dynamicTags.length) {
        this.$message.error('请选择执行人员')
        return
      }
      let a = []
      for (let item of this.dynamicTags) {
        a.push(item.id)
      }
      dataOption.user_ids = a.join(',')

      let b = []
      for (let item of this.readUserTags) {
        b.push(item.id)
      }
      dataOption.read_user = b.join(',')
      // name 	整型 	必填 	1 	- 	名称
      // user_ids 	整型 	必填 	1 	- 	用户id多个逗号隔开,
      let file = []
      for (let item of this.fileList) {
        let a = {}
        a.name = item.name
        a.src = item.src
        file.push(a)
      }
      dataOption.file = JSON.stringify(file)
      // console.log('编辑详情', dataOption)
      // let status = 1
      // if(status){
      //   return
      // }
      routineAdd(dataOption).then(res => {
        if (!res.code) {
          this.$message.success('编辑成功！')
          this.$emit('InitFn')
          this.closePOpup()
        } else {
          this.$message.error(res.msg)
        }
      })
    },
  },
  mounted() {
  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 50%;
  height: 550px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 20px;
  flex: 1;
  overflow-y: auto;
}

.close-Btn-line {
  cursor: pointer;
}

.hearder-search {
  display: flex;
  align-items: center;
}
</style>


